package com.dream.inspect.modules.business.controller;

import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.modules.business.entity.SuperviseStandardEntity;
import com.dream.inspect.modules.business.entity.SuperviseTempletEntity;
import com.dream.inspect.modules.business.service.SuperviseTempletService;
import com.dream.inspect.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 督察模板表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 17:04:54
 */
@RestController
@CrossOrigin
@RequestMapping("business/supervisetemplet")
public class SuperviseTempletController extends AbstractController {
    @Autowired
    private SuperviseTempletService superviseTempletService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:supervisetemplet:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("deptId", getDeptId());
        params.put("user", getUser());
        params.put("userId", getUserId());
        PageUtils page = superviseTempletService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/findAllModelList")
    @RequiresPermissions("business:supervisetemplet:list")
    public R findAllModelList(@RequestParam Map<String, Object> params) {
        //PageUtils page = superviseTempletService.queryPage(params);
        params.put("deptId", getDeptId());
        params.put("user", getUser());
        params.put("userId", getUserId());

        List<SuperviseTempletEntity> list = superviseTempletService.findAllModelList(params);
        return R.ok().put("list", list);
    }

    /**
     * 查询所有模板根据用户
     */
    @RequestMapping("/findAllModelToUserIdList")
    @RequiresPermissions("business:supervisetemplet:list")
    public R findAllModelToUserIdList(@RequestParam Map<String, Object> params) {
        //PageUtils page = superviseTempletService.queryPage(params);
        params.put("deptId", getDeptId());
        params.put("user", getUser());
        params.put("userId", getUserId());
        List<SuperviseTempletEntity> list = superviseTempletService.findAllModelToUserIdList(params);
        return R.ok().put("list", list);
    }

    /**
     * 列表查找模板对应的问题
     */
    @RequestMapping("/findModelStandardList")
    @RequiresPermissions("business:supervisetemplet:list")
    public R findModelStandardList(@RequestParam Map<String, Object> params) {
        List<SuperviseStandardEntity> list = superviseTempletService.findSuperviseStandardList(params);
        return R.ok().put("list", list);
    }

    /**
     * 列表查找模板对应的问题树
     */
    @RequestMapping("/findModelStandardListTree")
    @RequiresPermissions("business:supervisetemplet:list")
    public R findModelStandardListTree(@RequestParam Map<String, Object> params) {
        List<SuperviseStandardEntity> list = superviseTempletService.findModelStandardListTree(params);
        return R.ok().put("list", list);
    }

    @RequestMapping("/findModelStandardListTree1")
    public R findModelStandardListTree1(@RequestParam Map<String, Object> params) {
        List<SuperviseStandardEntity> list = superviseTempletService.findModelStandardListTree(params);
        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:supervisetemplet:info")
    public R info(@PathVariable("id") Long id) {
        SuperviseTempletEntity superviseTemplet = superviseTempletService.selectById(id);

        return R.ok().put("superviseTemplet", superviseTemplet);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:supervisetemplet:save")
    public R save(@RequestBody SuperviseTempletEntity superviseTemplet) {
        //superviseTempletService.insert(superviseTemplet);
        superviseTemplet.setCreateTime(new Date());
        superviseTemplet.setCreateUser(getUserId());
        superviseTempletService.addSuperviseTarget(superviseTemplet, getDeptId());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:supervisetemplet:update")
    public R update(@RequestBody SuperviseTempletEntity superviseTemplet) {
        ValidatorUtils.validateEntity(superviseTemplet);
        superviseTemplet.setUpdateTime(new Date());
        superviseTemplet.setUpdateUser(getUserId());
        superviseTempletService.updateById(superviseTemplet);//全部更新

        return R.ok();
    }

    /**
     * 启用
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("business:supervisetemplet:update")
    public R updateStatus(@RequestBody SuperviseTempletEntity superviseTemplet) {

        superviseTemplet.setUpdateTime(new Date());
        superviseTemplet.setUpdateUser(getUserId());
        superviseTemplet.setDeptId(getDeptId());
        Integer flag = superviseTempletService.updateStatus(superviseTemplet);//全部更新
        if (flag == 1) {
        return     R.error("1","存在已启用的每日必查模板！");
        }
        return R.ok();
    }
    /**
     * 存在启用，确认启用功能
     *
     * @param superviseTemplet
     */
    @RequestMapping("/updateStatusConfirm")
    @RequiresPermissions("business:supervisetemplet:update")
    public R updateStatusConfirm(@RequestBody SuperviseTempletEntity superviseTemplet) {

        superviseTemplet.setUpdateTime(new Date());
        superviseTemplet.setUpdateUser(getUserId());
        superviseTemplet.setDeptId(getDeptId());
       superviseTempletService.updateStatusConfirm(superviseTemplet);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:supervisetemplet:delete")
    public R delete(@RequestParam Map<String, Object> params) {

        //暂时不提供删除
        //superviseStandardService.deleteBatchIds(Arrays.asList(ids));
        SuperviseTempletEntity superviseTempletEntity = new SuperviseTempletEntity();
        superviseTempletEntity.setId(Long.valueOf(params.get("id") + ""));
        superviseTempletEntity.setDeleteFlag("1");
        superviseTempletEntity.setUpdateUser(getUserId());
        superviseTempletEntity.setUpdateTime(new Date());
        superviseTempletService.updateById(superviseTempletEntity);//全部更新
        return R.ok();
    }

}
